﻿using System;

namespace MahaBaseWrapper
{
    /// <summary>
    /// Represent the Insert Task
    /// </summary>
    /// <typeparam name="T">Entity to be inserted</typeparam>
    public sealed class Insert<T> where T : class
    {
        /// <summary>
        /// Current instace of QueryProcessor Class
        /// </summary>
        private Core.QueryProcessor<T> _processor;

        /// <summary>
        /// The constructor for Insert task, this is method allow to create a instance of the Insert class and perform insert against the database.
        /// </summary>
        public Insert()
        {
            _processor = new MahaBaseWrapper.Core.QueryProcessor<T>();
        }

        /// <summary>
        /// Method used to set values to the insert task.
        /// </summary>
        /// <param name="entity">Entity with wanted values to be inserted</param>
        /// <returns>The current instance of Insert class</returns>
        public Insert<T> Values(T entity)
        {
            this._processor.Values = new String[this._processor.Property.Length];

            for (int i = 0, z = this._processor.Property.Length; i < z; i++)
            {
                if (this._processor.Property[i].PropertyType == typeof(String))
                    this._processor.Values[i] = String.Concat("'", this._processor.Property[i].GetValue(entity, null).ToString(), "'");
                else
                    this._processor.Values[i] = this._processor.Property[i].GetValue(entity, null).ToString();
            }

            return this;
        }

        /// <summary>
        /// Method used to perform the configured insert against database.
        /// </summary>
        public void PerformInsert()
        {
            this._processor.PerformInsert();
        }
    }
}